<?php
namespace Home\Controller;
use Think\Controller;
class MobileController extends Controller {

        //初始化签到页
    public function index(){
          //构建签到页面
          //构建签到页面
          $span1="";
          for($i = strtotime('2016-12-24'); $i <= strtotime('2016-12-29'); $i += 86400) {
                $qd=GetQdBytime($i);
                if($qd!=null){
                      $span1.="<span id='$i' class=\"current\"></span>";
                }else{
                      $span1.="<span id='$i'></span>";
                }
          }
          $span2="";
          for($i = strtotime('2016-12-30'); $i <= strtotime('2017-1-10'); $i += 86400) {
                $qd=GetQdBytime($i);
                if($qd!=null){
                      $span2.="<span id='$i' class=\"current\"></span>";
                }else{
                      $span2.="<span id='$i'></span>";
                }
          }

          $span3="";
          for($i = strtotime('2017-1-11'); $i <= strtotime('2017-1-25'); $i += 86400) {
                $qd=GetQdBytime($i);
                if($qd!=null){
                      $span3.="<span id='$i' class=\"current\"></span>";
                }else{
                      $span3.="<span id='$i'></span>";
                }
          }
          $span4="";
          for($i = strtotime('2017-1-26'); $i <= strtotime('2017-2-10'); $i += 86400) {
                $qd=GetQdBytime($i);
                if($qd!=null){
                      $span4.="<span id='$i' class=\"current\"></span>";
                }else{
                      $span4.="<span id='$i'></span>";
                }
          }

          $span5="";
          for($i = strtotime('2017-2-11'); $i <= strtotime('2017-2-19'); $i += 86400) {
                $qd=GetQdBytime($i);
                if($qd!=null){
                      $span5.="<span id='$i' class=\"current\"></span>";
                }else{
                      $span5.="<span id='$i'></span>";
                }
          }
           $data['span1']=$span1;
           $data['span2']=$span2;
           $data['span3']=$span3;
           $data['span4']=$span4;
           $data['span5']=$span5;
           $this->ajaxReturn($data);// 赋值分页输出

    }

      //移动端跳转页面
      public  function  jumpToHtml(){
            $op=I("get.op");
            $uid = cookie('uid');
            $nick_name = cookie('nick_name');
            $login="";
            $SignStr="";
            if($uid!=null){
                  $score=GetUserSocreByUid($uid);
                  $login="<span>".$nick_name."</span><span id='score'>(积分".$score.")</span><a href=\"index.php?m=Home&c=mobile&a=Loginout\">退出</a>";
            }
            if($op=="integral"){
                  $NowDate=strtotime(date('Y-m-d H:i:s'));
                  $Start3=strtotime('2017-1-11 00:00:00');
                  $End3=strtotime('2017-1-25 23:59:59');
                  $Start4=strtotime('2017-1-26 00:00:00');
                  $End4=strtotime('2017-2-10 23:59:59');
                  $Start5=strtotime('2017-2-11 00:00:00');
                  $End5=strtotime('2018-2-19 23:59:59');
                if($NowDate>=$Start3 && $NowDate<=$End3){
                      $SignStr="<div class=\"inte_d03 \" id=\"qd3\">

					</div>
					<div class=\"inte_d04  fl\" id=\"qd4\">

					</div>
					<div class=\"inte_d05  fr\" id=\"qd5\">

					</div>
					<div class=\"inte_d02 fr\" id=\"qd2\">

					</div>
					<div class=\"inte_d01 fl\" id=\"qd1\">

					</div>";
                }
                  if($NowDate>=$Start4 && $NowDate<=$End4){
                        $SignStr="<div class=\"inte_d04\"  id=\"qd4\" style=\"width:100%;
	height:613px;\">

					</div>
					<div class=\"inte_d05  fl\" id=\"qd5\">

					</div>
					<div class=\"inte_d01 fr\" id=\"qd1\">

					</div>
					<div class=\"inte_d02 fl\" id=\"qd2\">

					</div>
					<div class=\"inte_d03 fr\" id=\"qd3\" style=\"width:48%;
	height:313px;\">

					</div>";
                  }
                  if($NowDate>=$Start5 && $NowDate<=$End5){
                        $SignStr="<div class=\"inte_d05  fl\" id=\"qd5\" style=\"width:100%;
	height:613px;\">

					</div>
					<div class=\"inte_d01 fr\" id=\"qd1\">

					</div>
					<div class=\"inte_d02 fl\" id=\"qd2\">

					</div>
					<div class=\"inte_d03 fl\" id=\"qd3\" style=\"width:48%;
	height:313px;\">

					</div>
					<div class=\"inte_d04 fr\" id=\"qd4\" style=\"width:48%;
	height:313px;\">

					</div>";
                  }

            }


            $this->assign('SignStr',$SignStr);// 赋值分页输出
            $this->assign('loginString',$login);// 赋值分页输出
            $this->display("mobile:".$op);
      }

      //移动退出登录
      public function Loginout(){
            cookie('uid',null); // 指定cookie保存时间
            cookie('nick_name',null); // 指定cookie保存时间
            cookie('email',null); // 指定cookie保存时间
            cookie('user_cell',null); // 指定cookie保存时间
            cookie('user_truename',null); // 指定cookie保存时间
            $login="<li><a href=\"index.php?m=Home&c=mobile&a=jumpToHtml&op=login\">登录</a></li>";
            $this->assign('login',$login);// 赋值分页输出
            $this->display("mobile:index");
      }

      /**
       * 移动签到
       */
      public  function  Sign(){
            if(strtotime(date('Y-m-d H:i:s'))>=strtotime('2017-2-19 12:59:59')){
                  $msg['status']  = 503;
                  $msg['content'] = '活动已经结束哦！';
                  $this->ajaxReturn($msg);
                  exit();
            }
            $id=cookie("uid");
            if($id==""){
                  $ajaxReturn['content']='您尚未登录!';
                  $this->ajaxReturn($ajaxReturn);
            }
            //判断今天是否签到过
            $Sign=D('Sign');
            $where['create_time']=strtotime(date("Y-m-d"));
            $where['uid']=$id;
            $CkTodayHaveSign=$Sign->where($where)->find();
            if($CkTodayHaveSign || count($CkTodayHaveSign)>0){
                  \Think\Log::write('今天已经签到过 签到时间为：'.date("Y-m-d"),'INFO');
                  $ajaxReturn['nowDate']=strtotime(date("Y-m-d"));
                  $ajaxReturn['score']=0;
                  $ajaxReturn['content']='您今天已经签到过，明天再来吧!';
                  $this->ajaxReturn($ajaxReturn);
                  exit;
            }
            unset($st);
            unset($Sign);
            //检查是否是连续签到  只要判断最后签到的天数 和现在的时间相差>1就说明不是连续签到
            $Sign=D('Sign');
            $where2['uid']=$id;
            $CkContinueSign=$Sign->where($where2)->order('create_time desc')->select();
            unset($Sign);
            if($CkContinueSign ||count($CkContinueSign)>0){ //检查是否连续签到
                  $time= $CkContinueSign[0]['create_time'];
                  \Think\Log::write('最后一次签到的时间 签到时间为：'.date("Y-m-d",$time),'INFO');
//                  echo "最后一次签到的时间".date("Y-m-d",$time);
                  $NowTime= strtotime(date("Y-m-d"));
//                  $diff=  $NowTime-$time;
                  $diff=  diffBetweenTwoDays($time,$NowTime);
                  \Think\Log::write("最后签到的时间和今天相差天数为".$diff.'INFO');
                  if($diff==1){
//                       echo "持续签到加积分操作";
                        //新增一条 签到记录
                        writeSign($id,null);
                        //获取持续签到的天数
                        $Sign=D('Sign');
                        $where3['uid']=$id;
                        $where3['isbreak']=1;
                        $CkContinueSign2=$Sign->where($where3)->order('create_time desc')->find();
                        if($CkContinueSign2 || count($CkContinueSign2)>0){
                              $time2= $CkContinueSign2['create_time'];
                              \Think\Log::write("最后一次断签时间".date("Y-m-d",$time2),'INFO');
                              \Think\Log::write("当前时间".date("Y-m-d",$NowTime).'INFO');
//                              $diff23= $NowTime-$time2;
                              $diff24= diffBetweenTwoDays($time2,$NowTime);
                              \Think\Log::write("断签的天数和现在的时间相差".$diff24."天",'INFO');
                              Add_user_points($id,$diff24+1,1,155);
//                               echo "您今天已经签到过，明天再来吧";
                              \Think\Log::write('签到时间为：'.date("Y-m-d",$time),'INFO');
                              $ajaxReturn['score']=$diff24+1;
                              $ajaxReturn['nowDate']=strtotime(date("Y-m-d"));
                              $ajaxReturn['content']=($diff24+1).'积分入囊中<br/>随意花';
                              $this->ajaxReturn($ajaxReturn);
                        }else{
                              Add_user_points($id,count($CkContinueSign)+1,1,155);
//                               echo "您今天已经签到过，明天再来吧";
                              $ajaxReturn['score']=count($CkContinueSign)+1;
                              $ajaxReturn['nowDate']=strtotime(date("Y-m-d"));
                              $ajaxReturn['content']=(count($CkContinueSign)+1).'积分入囊中<br/>随意花!';
                              $this->ajaxReturn($ajaxReturn);
                        }
                  }else{
//                       echo "断签签到加积分操作";
                        //新增一条 签到记录
                        writeSign($id,1);
                        Add_user_points($id,1,1,155);
//                         echo "您今天已经签到过，明天再来吧";
                        $ajaxReturn['score']=1;
                        $ajaxReturn['nowDate']=strtotime(date("Y-m-d"));
                        $ajaxReturn['content']='1积分入囊中<br/>随意花!';
                        $this->ajaxReturn($ajaxReturn);
                  }
            }else{//第一次签到
//                       echo "我是第一次签到";
                  \Think\Log::write($id.'我是第一次签到：'.date("Y-m-d"),'INFO');
                  //新增一条 签到记录
                  Add_user_points($id,1,1,155);
                  writeSign($id,null);
                  $ajaxReturn['score']=1;
                  $ajaxReturn['nowDate']=strtotime(date("Y-m-d"));
                  $ajaxReturn['content']="1积分入囊中<br/>随意花!";
                  $this->ajaxReturn($ajaxReturn);
            }
      }

    /**
     * 获取获奖名单
     */
    public function getAllActPrize(){
        $actPrize = get_all_act_prize_mobile();

        $prizes = array();
        $user= D('User');
        $exchangem= M('exchange','ny_prize_','newyear');
        $lottom= M('lotto','ny_prize_','newyear');
        foreach($actPrize['prizes'] as $prize){
            $userWhere['id'] = $prize['uid'];
            $userinfo = $user->where($userWhere)->select();
            $prize['username']=$userinfo[0]['user_name'];
            $prizeWhere['id'] = $prize['pid'];
            if($prize['type']==1){//兑奖
                $prize['type']="兑奖";
                $exchange=$exchangem->where($prizeWhere)->select();
                $prize['pname']=$exchange[0]['pname'];
            }
            if($prize['type']==2){//抽奖
                $lotto=$lottom->where($prizeWhere)->select();
                $prize['pname']=$lotto[0]['pname'];
                $prize['type']="抽奖";
            }
            $prize['subtime'] = date("Y-m-d", $prize['subtime']);
            array_push($prizes,$prize);
        }
        $actPrize['prizes'] = $prizes;
        $actPrize['status'] = 200;
        $this->ajaxReturn($actPrize,'JSON');
    }

    /**
     * 获取用户的礼品
     */
    public function getUserActPrize(){
        if(checkIsLogin()){
            $msg['status']  = 504;
            $msg['msg'] = '您尚未登陆！';
            $this->ajaxReturn($msg);
            exit();
        }
        $actPrize = get_user_act_prize_mobile();

        $prizes = array();
        $exchangem= M('exchange','ny_prize_','newyear');
        $lottom= M('lotto','ny_prize_','newyear');
        foreach($actPrize['prizes'] as $prize){
            $prizeWhere['id'] = $prize['pid'];
            if($prize['type']==1){//兑奖
                $exchange=$exchangem->where($prizeWhere)->select();
                $prize['pname']=$exchange[0]['pname'];
                $prize['img']=$exchange[0]['image'];
                $prize['explain']=$exchange[0]['explain'];
                $prize['type']="兑奖";
            }
            if($prize['type']==2){//抽奖
                $lotto=$lottom->where($prizeWhere)->select();
                $prize['pname']=$lotto[0]['pname'];
                $prize['img']=$lotto[0]['image'];
                $prize['explain']=$lotto[0]['explain'];
                $prize['type']="抽奖";
            }
            if($prize['code_url']==null){
                $prize['code_url']="———";
            }
            if($prize['code']==null){
                $prize['code']=$prize['code_url'];
            }
            $prize['subtime'] = date("Y-m-d", $prize['subtime']);
            array_push($prizes,$prize);
        }

        $user= D('User');
        $userWhere['id'] = cookie('uid');
        $userinfo = $user->where($userWhere)->select();

        $actPrize['prizes'] = $prizes;
        $actPrize['user'] = $userinfo;
        $actPrize['status'] = 200;
        $this->ajaxReturn($actPrize,'JSON');
    }

      public  function  Get_convert_prize(){

            $pages = array();
            $M= M('exchange','ny_prize_','newyear');
            $count      = $M->count();// 查询满足要求的总记录数
            $Page       = new \Think\PageMobile($count,4);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $show       = $Page->show();// 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $prizes = $M->order("id not in(12,13), score desc")->limit($Page->firstRow.','.$Page->listRows)->select();

            $pages['show'] = $show;
            $pages['prizes'] = $prizes;
            $this->ajaxReturn($pages,'Json');

      }
    /**
     * 获取经销商省份信息
     */
    public function getDealerPrivince(){
        $dealerPrivince=get_dealer_privince();
        $this->ajaxReturn($dealerPrivince,'JSON');
    }

    /**
     * 获取经销商城市信息
     */
    public function getDealerCity(){
        $privince= I('post.privince');
        $dealerCity=get_dealer_city_from_privince($privince);
        $this->ajaxReturn($dealerCity,'JSON');
    }

    /**
     * 获取用户的信息
     */
    public function getUserInfo(){

        if(checkIsLogin()){
            $msg['status']  = 504;
            $msg['msg'] = '您尚未登陆！';
            $this->ajaxReturn($msg);
            exit();
        }

        $userinfos = array();
        $user= D('User');
        $userWhere['id'] = cookie('uid');
        $userinfo = $user->where($userWhere)->find();

        $userinfos['user'] = $userinfo;
        $userinfos['status']  = 200;

        $this->ajaxReturn($userinfos,'JSON');
    }

    /**
     * 获取省
     */
    public function getCmsLinkageP(){

        $M= M('linkage','cms_','newyear');
        $where_p['parentid'] = 0;
        $add_p=$M->cache(true)->where($where_p)->order('convert(name using gb2312)')->select();

        $this->ajaxReturn($add_p,'JSON');
    }
    /**
     *
     * 改变城市
     */
    public  function  changeCity(){
        $proid= I('post.proid');
        $M= M('linkage','cms_','newyear');
        \Think\Log::record($proid,'INFO');
        $where_c['parentid'] = $proid;
        $add_c=$M->cache(true)->where($where_c)->order('convert(name using gb2312)')->select();

        $this->ajaxReturn($add_c,'JSON');
    }

    /**
     * 保存用户邮寄地址
     *
     */
    public  function  saveUserInfo(){
        if(checkIsLogin()){
            $msg['status']  = 504;
            $msg['msg'] = '您尚未登陆！';
            $this->ajaxReturn($msg);
            exit();
        }
        $pro= I('post.pro');
        $phone= I('post.phone');
        $city=  I('post.city');
        $username= I('post.username');
        $address= I('post.address');

        $M = M('user','ny_','newyear');
        $data['add_p'] = $pro;
        $data['add_c'] = $city;
        $data['user_turename']=$username;
        $data['phone']=$phone;
        $data['address']=$address;
        $M->where("id=".cookie("uid"))->save($data);

        $msg['status']=200;
        $msg['msg']="保存成功！";
        $this->ajaxReturn($msg);
    }
}